package pcjavanet.mobilesell.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pcjavanet.mobilesell.model.Authorizations;
import pcjavanet.mobilesell.model.AuthorizationsExample;
import pcjavanet.mobilesell.model.Employee;
import pcjavanet.mobilesell.model.EmployeeExample;
import pcjavanet.mobilesell.model.Module;
import pcjavanet.mobilesell.model.ModuleExample;
import pcjavanet.mobilesell.model.PageBean;
import pcjavanet.mobilesell.model.QryCondition;
import pcjavanet.mobilesell.persistence.AuthorizationsMapper;
import pcjavanet.mobilesell.persistence.EmployeeMapper;
import pcjavanet.mobilesell.persistence.ModuleMapper;


@Service
public class AuthorizationsService {


  @Autowired
  private  AuthorizationsMapper authorizationsMapper;
  @Autowired
  private  ModuleMapper moduleMapper;
  @Autowired
  private  EmployeeMapper employeeMapper;


  public int  insert( Authorizations authorizations ) throws Exception{
	return authorizationsMapper.insert (authorizations);
  }


  public Authorizations  selectByPrimaryKey( Authorizations  bean ) {
 	int dbid = bean.getDbid();
	Authorizations authorizations = authorizationsMapper.selectByPrimaryKey( dbid );
	Module module = moduleMapper.selectByPrimaryKey( authorizations.getRefModuleDbid() );
	authorizations.setModule(module) ;
	Employee employee = employeeMapper.selectByPrimaryKey( authorizations.getRefEmployeeDbid() );
	authorizations.setEmployee(employee) ;
	return authorizations ;
  }


  public int  updateByPrimaryKeySelective( Authorizations authorizations ) throws Exception{
	return authorizationsMapper.updateByPrimaryKeySelective( authorizations );
  }


  public int  deleteByPrimaryKey( Authorizations  bean ) throws Exception{
 	int dbid = bean.getDbid();
	return authorizationsMapper.deleteByPrimaryKey( dbid );
  }


  public List<Authorizations>  selectByExample( Authorizations authorizations ) throws Exception{
    AuthorizationsExample example = new AuthorizationsExample();
	return authorizationsMapper.selectByExample( example );
  }
  
  public List<Module>  listAllModule(  ) throws Exception{
	  ModuleExample example = new ModuleExample();
	  return moduleMapper.selectByExample( example );
  }

  public List<Authorizations>  getOnePageBeans( QryCondition condition , PageBean pageBean ) throws Exception{
    AuthorizationsExample example = new AuthorizationsExample();
    ServiceUtil.dealCriteria( condition,  example.createCriteria() );
	List<Authorizations> ls=authorizationsMapper.getOnePageBeans( example , pageBean );
//begin
	if ( ls.size() > 0 ) {
  		Map<Integer,Integer> moduleIdmap = new HashMap<Integer,Integer>();
  		for(Authorizations  m : ls){
  			m.setModule(new Module ()) ; 
  			if ( m.getRefModuleDbid() != null ) 
  				moduleIdmap.put(m.getRefModuleDbid(), m.getRefModuleDbid());
  		};
  		List<Integer> refModuleDbids = new ArrayList<Integer>();
  		Iterator<Integer> refModuleDbidit = moduleIdmap.keySet().iterator(); 
  		while ( refModuleDbidit.hasNext() ){
  			refModuleDbids.add( refModuleDbidit.next() );
  		}
  		ModuleExample moduleExample = new ModuleExample();
  		if ( refModuleDbids.size() > 0 ) 
  			moduleExample.createCriteria().andDbidIn( refModuleDbids );
  		List<Module> modules = moduleMapper.selectByExample( moduleExample);
  		for(Authorizations m : ls ){
  			for(Module c : modules ){
  				if ( m.getRefModuleDbid() !=null && c.getDbid() !=null && m.getRefModuleDbid().intValue() == c.getDbid().intValue() ) {
  					m.setModule( c );
  				}
  			}
  		}
  		Map<Integer,Integer> employeeIdmap = new HashMap<Integer,Integer>();
  		for(Authorizations  m : ls){
  			m.setEmployee(new Employee ()) ; 
  			if ( m.getRefEmployeeDbid() != null ) 
  				employeeIdmap.put(m.getRefEmployeeDbid(), m.getRefEmployeeDbid());
  		};
  		List<Integer> refEmployeeDbids = new ArrayList<Integer>();
  		Iterator<Integer> refEmployeeDbidit = employeeIdmap.keySet().iterator(); 
  		while ( refEmployeeDbidit.hasNext() ){
  			refEmployeeDbids.add( refEmployeeDbidit.next() );
  		}
  		EmployeeExample employeeExample = new EmployeeExample();
  		if ( refEmployeeDbids.size() > 0 ) 
  			employeeExample.createCriteria().andDbidIn( refEmployeeDbids );
  		List<Employee> employees = employeeMapper.selectByExample( employeeExample);
  		for(Authorizations m : ls ){
  			for(Employee c : employees ){
  				if ( m.getRefEmployeeDbid() !=null && c.getDbid() !=null && m.getRefEmployeeDbid().intValue() == c.getDbid().intValue() ) {
  					m.setEmployee( c );
  				}
  			}
  		}
	}
//end
	return ls;
  }

  public int getTotalRecords( QryCondition condition , PageBean pageBean ) throws Exception{
    AuthorizationsExample example = new AuthorizationsExample();
    ServiceUtil.dealCriteria( condition,  example.createCriteria() );
	return authorizationsMapper.getTotalRecords( example , pageBean );
  }

  public int countByExample( Authorizations authorizations , PageBean pageBean ){
    AuthorizationsExample example = new AuthorizationsExample();
	return authorizationsMapper.countByExample( example );
  }
  public List<Authorizations> selectByExamplePage( Authorizations authorizations , PageBean pageBean ){
  AuthorizationsExample example = new AuthorizationsExample();
	return authorizationsMapper.selectByExamplePage( example , pageBean );
  }

}
